package edu.boun.g4.coursity.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.Jsoup;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.boun.g4.coursity.domain.Course;
import edu.boun.g4.coursity.dto.CourseDto;
import edu.boun.g4.coursity.service.CommentService;
import edu.boun.g4.coursity.service.CourseService;
import edu.boun.g4.coursity.service.InitiativeService;
import edu.boun.g4.coursity.service.InstitutionService;
import edu.boun.g4.coursity.service.InstructorService;
import edu.boun.g4.coursity.service.LanguageService;
import edu.boun.g4.coursity.service.SemanticTagService;

/**
 * 
 * @author mertusta
 * 
 */
@Controller
@RequestMapping
public class SearchController {

	@Resource(name = "courseService")
	private CourseService courseService;

	@Resource(name = "initiativeService")
	private InitiativeService initiativeService;

	@Resource(name = "institutionService")
	private InstitutionService institutionService;

	@Resource(name = "instructorService")
	private InstructorService instructorService;

	@Resource(name = "languageService")
	private LanguageService languageService;

	@Resource(name = "commentService")
	private CommentService commentService;

	@Resource(name = "semanticTagService")
	private SemanticTagService semanticTagService;

	@RequestMapping(method = RequestMethod.POST, value = "/search")
	public @ResponseBody
	Response listCourses(@RequestParam String searchParam) {

		List<Course> all = courseService.getAll("");
		List<CourseDto> dtos = new ArrayList<CourseDto>();

		CourseDto dto;
		for (Course course : all) {
			dto = new CourseDto();
			dto.setCategories(semanticTagService.getAllCategories(course
					.getCourseId()));
			dto.setComments(commentService.getAll(course.getCourseId()));
			dto.setCountry(course.getCountry());
			dto.setCourseDescription(Jsoup.parse(course.getCourseDescription())
					.text().substring(0, 100)
					+ "...");
			dto.setCourseId(course.getCourseId());
			dto.setCourseName(course.getCourseName());
			dto.setEstimatedEffort(course.getEstimatedEffort());
			dto.setExam(course.isExam());
			dto.setForum(course.isForum());
			dto.setInfo(course.getInfo());
			dto.setInitiative(initiativeService.getAll(course.getCourseId()));
			dto.setInstitution(institutionService.getAll(course.getCourseId()));
			dto.setInstructor(instructorService.getAll(course.getCourseId()));
			dto.setLanguages(course.getLanguages());
			dto.setLength(course.getLength());
			dto.setPeerAssesment(course.isPeerAssesment());
			dto.setPrerequisites(courseService.getAllPrerequisite(course
					.getCourseId()));
			dto.setRating(course.getRating());
			dto.setRecommendedBackground(course.getRecommendedBackground());
			dto.setStartingDate(course.getStartingDate());
			dto.setTeamProjects(course.isTeamProjects());
			dto.setTextbook(course.getTextbook());
			dto.setUrl(course.getUrl());
			dto.setVideoLectures(course.isVideoLectures());

			dtos.add(dto);
		}

		Response response = new Response();
		response.setBody(dtos);
		response.setStatus(true);

		return response;
	}
}
